package com.example.s08restful_sagger.controller;

import com.example.s08restful_sagger.pojo.BaseResult;
import com.example.s08restful_sagger.pojo.Message;
import com.example.s08restful_sagger.service.IMessageService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @auther Qiang
 * @date 2022/4/3 15:13
 */

@Api(value="消息",description = "消息api",position = 100,protocols = "http")
@RestController
@RequestMapping("/message/")
public class MessageController {

    @Autowired
    private IMessageService messageService;

    // 查找
    @ApiOperation(
            value = "消息列表",
            notes = "完整的消息内容列表",
            produces="application/json, application/xml",
            consumes="application/json, application/xml",
            response = List.class)
    @GetMapping("/")
    public BaseResult<List<Message>> list()
    {
        return  BaseResult.success(messageService.findAll());
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据编号查找消息", notes = "根据编号查找消息")
    public BaseResult<Message> findOne(@PathVariable("id") Long id)
    {
        return BaseResult.success(messageService.findById(id));
    }

    // 增加 POST
    // 参数在请求体
//    id=4&text=好消息&summary=哈哈

//    @PostMapping("/")
//    public  Message add(Message m)
//    {
//        this.messageService.save(m);
//        return m;
//    }

    // 参数在请求体 json格式
    @ApiOperation(value = "添加消息", notes = "根据参数创建消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "消息 ID", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "text", value = "正文", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "摘要", required = false, dataType = "String", paramType = "query"),
    })
    @PostMapping("/")
    public  BaseResult<Message> add(@RequestBody Message m)
    {
        this.messageService.save(m);
        return BaseResult.success(m);
    }

    // 删除 DETETE
    @DeleteMapping(value = "message/{id}")
    @ApiOperation(value = "删除消息", notes = "根据参数修改消息")
    public void delete(@PathVariable("id") Long id) {
        this.messageService.removeById(id);
    }

    // 修改 PUT PATCH

    // 使用 put 请求进行修改
    @PutMapping(value = "message")
    @ApiOperation(value = "修改消息", notes = "根据参数修改消息")
    @ApiResponses({
            @ApiResponse(code = 100, message = "请求参数有误"),
            @ApiResponse(code = 101, message = "未授权"),
            @ApiResponse(code = 103, message = "禁止访问"),
            @ApiResponse(code = 104, message = "请求路径不存在"),
            @ApiResponse(code = 200, message = "服务器内部错误")
    })
    public BaseResult<Message> modify(Message message) {
        this.messageService.updateById(message);
        return BaseResult.success(message);
    }

    // 更新消息的 text 字段
    @PatchMapping(value="/message/text")
    @ApiOperation(value = "修改消息内容", notes = "根据参数修改消息内容")
    public BaseResult<Message> patch(Message message) {
        this.messageService.updateText(message);
        return BaseResult.success(message);
    }
}


